// index.js
Page({
    data: {
        imageList: [], // 已选择的图片列表
        uploadProgress: 0 // 上传进度
    },

    // 选择图片
    chooseImage() {
        wx.chooseImage({
            count: 1, // 最多选择1张图片
            sizeType: ['compressed'], // 压缩图
            sourceType: ['album', 'camera'], // 来源相册或相机
            success: (res) => {
                const tempFilePaths = res.tempFilePaths
                this.setData({
                    imageList: this.data.imageList.concat(tempFilePaths)
                })
            }
        })
    },

    // 预览图片
    previewImage(e) {
        const index = e.currentTarget.dataset.index
        wx.previewImage({
            current: this.data.imageList[index], // 当前显示图片
            urls: this.data.imageList // 所有图片
        })
    },
    // 上传图片到阿里云OSS
    uploadToOSS() {
        if (this.data.imageList.length === 0) {
            wx.showToast({
                title: '请先选择图片',
                icon: 'none'
            })
            return
        }
        try {
            var qrOssUrl = ''
            // 上传第一张图片
            var  filePath = this.data.imageList[0]
            var fileDir='t/image/temp'
            var fileName = Date.parse(new Date()) + filePath.substr(filePath.lastIndexOf('/'))
            var ossKey = `${fileDir}${Date.now()}_${fileName}`
            wx.uploadFile({
                    url: 'https://coolcdn.igetcool.com',
                    filePath: filePath,
                    name: 'file',
                    header: {
                        "Content-Type": "multipart/form-data"
                    },
                    formData: {
                        name: fileName,
                        key: ossKey,
                        policy: 'eyJleHBpcmF0aW9uIjoiMjAyNS0wOS0xNVQxMDozOTo0NC42NTZaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ0L2ltYWdlL3RlbXAiXV19',
                        OSSAccessKeyId: 'LTAI4GCHtZybhSTskRiW384j',
                        signature: '1J+o4lI0Ftw8qotVqHXSAL82AIo=',
                        success_action_status: '200'
                    },
                    success: (res) => {
                        if (res.statusCode === 200) {
                            qrOssUrl = `https://coolcdn.igetcool.com/${ossKey}`
                            console.log('OSS URL:', qrOssUrl)
                            //解析课程ID
                            this.parseCourseId(qrOssUrl)
                        } else {
                            console.error('上传文件请求失败:', res)
                            wx.showToast({
                                title: '上传文件失败',
                                icon: 'error'
                            })
                        }
                    },
                    fail: (err) => {
                        console.error('上传文件请求失败:', err)
                        wx.showToast({
                            title: '网络错误',
                            icon: 'error'
                        })
                    }
                })
   

            }catch (error) {
                console.error('二维码识别失败:', error)
                wx.showToast({
                    title: '二维码识别失败',
                    icon: 'error'
                })
            }
        },

        //解析二维码生产链接
        parseCourseId(qrOssUrl){
            console.log("解析课程:", qrOssUrl)
             if(qrOssUrl.length === 0){
                return
            }
            wx.request({
              url: 'https://api.2dcode.biz/v1/read-qr-code',
              data:{
                file_url:`${qrOssUrl}`
              },
              success:(res) => {
                if (res.statusCode === 200) {
                    var qrUrl = res.data.data.contents[0];
                    var courseId = qrUrl.substr(qrUrl.length - 5);
                    console.log('courseId:', courseId);
                    this.appointCourse(courseId);
                }
              }
            })
        },

        //多线程预约课程
        appointCourse(courseId){
            
        },

        // 删除图片
        deleteImage(e) {
            const index = e.currentTarget.dataset.index
            const newImageList = [...this.data.imageList]
            newImageList.splice(index, 1)
            this.setData({
                imageList: newImageList
            })
        }
    })